/*
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*/

import QtQuick 1.1
import NearFieldItem 1.0

Rectangle {
    id: id_screen
    anchors.fill: parent

    property bool active: Qt.application.active

    function randomNdefRecordPayload() {
        var payload = Math.round(Math.random() * 9999).toString();

        if (payload.length < 4) {
            payload = "000" + payload;
            payload = payload.substr(payload.length - 4);
        }

       return payload;
    }

    onActiveChanged: {
        if (Qt.application.active) {
            id_nearFieldItem.readSharedMemory();
        }
    }

    NearFieldItem {
        id: id_nearFieldItem
        ndefRecordType: "www.developer.nokia.com:example"
        ndefRecordPayload: randomNdefRecordPayload()

        onTargetAccessModeChanged: {
            switch (targetAccessMode) {
            case NearFieldItem.NoTargetAccess:
                id_targetAccessModeText.text = qsTr("none");
                break;
            case NearFieldItem.NdefReadTargetAccess:
                id_targetAccessModeText.text = qsTr("reader");
                break;
            case NearFieldItem.NdefWriteTargetAccess:
                id_targetAccessModeText.text = qsTr("writer");
                break;
            }
        }
        onTargetDetected: {
            id_targetAccessText.text = qsTr("target detected");
        }
        onTargetLost: {
            id_targetAccessText.text = qsTr("target lost");
        }
        onNdefMessageRead: {
            id_targetAccessText.text = qsTr("tag read");
        }
        onNdefMessageWritten: {
            id_targetAccessText.text = qsTr("tag written");
        }
    }

    ListModel {
        id: id_listModel
        ListElement {
            targetAccessMode: NearFieldItem.NdefReadTargetAccess
            color: "green"
        }
        ListElement {
            targetAccessMode: NearFieldItem.NdefWriteTargetAccess
            color: "red"
        }
//        ListElement {
//            targetAccessMode: NearFieldItem.NoTargetAccess
//            color: "blue"
//        }
    }

    ListView {
        id: id_listView
        anchors.fill: parent
        model: id_listModel
        delegate: id_listDelegate
        snapMode: ListView.SnapOneItem

        Component.onCompleted: {
            var targetAccessMode = model.get(0).targetAccessMode;

            if (targetAccessMode != undefined) {
                id_nearFieldItem.setTargetAccessMode(targetAccessMode);
            }
        }

        onMovementStarted: {

        }

        onMovementEnded: {
            var index = indexAt(contentX, contentY);
            var targetAccessMode = model.get(index).targetAccessMode;

            if (targetAccessMode != undefined) {
                id_nearFieldItem.setTargetAccessMode(targetAccessMode);
            }

            id_targetAccessText.text = "";
        }
    }

    Component {
        id: id_listDelegate
        Rectangle {
            id: id_listDelegateRectangle
            width: id_screen.width
            height: id_screen.height
            color: model.color

            MouseArea {
                anchors.fill: parent
                onPressAndHold: {
                    Qt.quit();
                }
            }
            Column {
                anchors.centerIn: parent
                Text {
                    color: "white"
                    elide: Text.ElideMiddle
                    horizontalAlignment: Text.AlignHCenter
                    text: id_nearFieldItem.ndefRecordType
                }
                Text {
                    id: id_ndefRecordPayloadText
                    anchors {
                        left: parent.left
                        right: parent.right
                    }

                    font {
                        pixelSize: Math.min(id_screen.width, id_screen.height) / 4
                    }
                    color: "white"
                    text: id_nearFieldItem.ndefRecordPayload
                    horizontalAlignment: Text.AlignHCenter

                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            id_nearFieldItem.ndefRecordPayload = randomNdefRecordPayload();
                        }
                    }
                }
            }

        }
    }

    Text {
        id: id_targetAccessModeText
        anchors {
            left: parent.left
            right: parent.right
            top: parent.top
            margins: 4.0
        }
        font {
            pixelSize: Math.min(id_screen.width, id_screen.height) / 6
        }
        color: "white"
        horizontalAlignment: Text.AlignLeft
    }
    Text {
        id: id_targetAccessText;
        anchors {
            left: parent.left
            right: parent.right
            top: id_targetAccessModeText.bottom
            margins: 6.0
        }
        color: "white"
        horizontalAlignment: Text.AlignLeft
    }

    Text {
        id: id_debugText;
        anchors {
            left: parent.left
            right: parent.right
            bottom: parent.bottom
            margins: 5.0
        }
        color: "white"
        maximumLineCount: 3
        horizontalAlignment: Text.AlignRight
        wrapMode: Text.Wrap
        //text: id_nearFieldItem.targetAccessMode + " " + id_nearFieldItem.errorCategory + " " + id_nearFieldItem.error
        text: qsTr("tap, flick, touch, long tap")
    }
}
